package com.ziyun.erp.modules.quoteAttribute.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ziyun.erp.modules.goods.dto.OptionDto;
import com.ziyun.erp.modules.quoteAttribute.dao.QuoteAttributeOptionDao;
import com.ziyun.erp.modules.quoteAttribute.entity.QuoteAttributeOptionEntity;
import com.ziyun.erp.modules.quoteAttribute.service.QuoteAttributeOptionService;
import com.ziyun.erp.modules.quoteFactoryMaterial.entity.QuoteFactoryMaterialEntity;
import com.ziyun.erp.modules.quoteFactoryMaterial.service.QuoteFactoryMaterialService;
import io.renren.common.annotation.SysLog;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("quoteAttributeOptionService")
public class QuoteAttributeOptionServiceImpl extends ServiceImpl<QuoteAttributeOptionDao, QuoteAttributeOptionEntity> implements QuoteAttributeOptionService {

    @Autowired
    private QuoteAttributeOptionDao quoteAttributeOptionDao;
    @Autowired
    private QuoteFactoryMaterialService quoteFactoryMaterialService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<QuoteAttributeOptionEntity> page = this.selectPage(
                new Query<QuoteAttributeOptionEntity>(params).getPage(),
                new EntityWrapper<QuoteAttributeOptionEntity>().eq("option_attribute_id", params.get("id")).eq("del_flag", "false")
                        .like(params.get("optionName") != null, "option_name", (String) params.get("optionName")).groupBy("option_name")
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public R insertQuoteAttributeOptionEntitys(QuoteAttributeOptionEntity quoteAttributeOption) {
        quoteAttributeOption.setOptionAttributeId("1");
        quoteAttributeOption.setCreateDate(new Date());
        if ("[]".equals(quoteAttributeOption.getOptionDefinitionParam())||"[[]]".equals(quoteAttributeOption.getOptionDefinitionParam())) {
            quoteAttributeOption.setOptionDefinitionParam("");
        }
        if ("无".equals(quoteAttributeOption.getOptionName())) {// 当是无的时候设置成none
            quoteAttributeOption.setOptionKey("none");
        }
        if (quoteAttributeOption.getOptionKey() == null || quoteAttributeOption.getOptionKey() == "") {// 当不需要区分key的时候设置成same
            quoteAttributeOption.setOptionKey("same");
        }
        String printModeType = "single,back,roll";

        if (quoteAttributeOption.getReprints() != null) {
            if (quoteAttributeOption.getReprints()) {
                printModeType += ",reprints";
            }
            quoteAttributeOption.setPrintModeType(printModeType);
        }
        //根据选项名称查询该克重是否已经存在
        int count = quoteAttributeOptionDao.getOptionName2Count(quoteAttributeOption.getOptionName(), quoteAttributeOption.getOptionName2());
        if (count > 0) {
            return R.error("该克重已经存在!");
        }
        boolean result = this.insert(quoteAttributeOption);
        if (!result) {
            R.error("添加失败!");
        }
        return R.ok();
    }

    @Override
    public R deleteByIds(Integer[] ids) {
        quoteAttributeOptionDao.deleteByIds(ids);
        return R.ok();
    }

    @Override
    public List<QuoteAttributeOptionEntity> getMaterialList() {
        return quoteAttributeOptionDao.getMaterialList();
    }

    @Override
    /**
     * 根据partsid和attributeId获得模板表的options
     */
    public List<QuoteAttributeOptionEntity> selectTemplateOptions(Integer partsId, Integer attributeId) {
        return quoteAttributeOptionDao.selectTemplateOptions(partsId, attributeId);
    }

    @Override
    public List<OptionDto> selectHasCraftPriceOption(Integer sellerId, String optionOrigin, String optionIds) {
        return quoteAttributeOptionDao.selectHasCraftPriceOption(sellerId, optionOrigin, optionIds);
    }

    @Override
    public List<QuoteAttributeOptionEntity> selectTemplateOptionsNew(Integer categoryId, Integer attributeId) {
        return quoteAttributeOptionDao.selectTemplateOptionsNew(categoryId, attributeId);
    }

    @Override
    public int getOptionNameCount(String optionName) {
        return quoteAttributeOptionDao.getOptionNameCount(optionName);
    }

    @Override
    public List<Map<String, Object>> getGramdepthsByOptionName(String optionName) {
        return quoteAttributeOptionDao.getGramdepthsByOptionName(optionName);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateList(List<QuoteAttributeOptionEntity> quoteAttributeOptions) {
        for (QuoteAttributeOptionEntity quoteAttributeOption : quoteAttributeOptions) {
            if (quoteAttributeOption.getId() == null) {
                //新增
                //根据选项名称查询该克重是否已经存在
                int count = quoteAttributeOptionDao.getOptionName2Count(quoteAttributeOption.getOptionName(), quoteAttributeOption.getOptionName2());
                if (count > 0) {
                    return R.error("该克重已经存在!");
                }
                if ("[]".equals(quoteAttributeOption.getOptionDefinitionParam())||"[[]]".equals(quoteAttributeOption.getOptionDefinitionParam())){
                    quoteAttributeOption.setOptionDefinitionParam("");
                }
                this.baseMapper.insert(quoteAttributeOption);
            }else {
                //修改
                this.updateById(quoteAttributeOption);
            }
        }
        return R.ok();
    }

    @Override
    public R deleteAll(String optionName) {
        this.baseMapper.deleteAll(optionName);
        return R.ok();
    }

        @Override
    public Boolean insertQuoteAttributeOptionEntity(QuoteAttributeOptionEntity quoteAttributeOption) {
        int count = this.selectCount(new EntityWrapper<QuoteAttributeOptionEntity>().eq("option_attribute_id", quoteAttributeOption.getOptionAttributeId())
                .eq("option_name", quoteAttributeOption.getOptionName()).eq("del_flag",false));
        if (count > 0) {
            throw new RRException("此属性的选项已存在!");
        }
        quoteAttributeOption.setCreateDate(new Date());
        if (StringUtils.isNotBlank(quoteAttributeOption.getOptionDefinitionParam())) {
            quoteAttributeOption.setOptionDefinitionParam(quoteAttributeOption.getOptionDefinitionParam().replaceAll("\"", ""));
        }
        if ("无".equals(quoteAttributeOption.getOptionName())) {// 当是无的时候设置成none
            quoteAttributeOption.setOptionKey("none");
        }
        if (quoteAttributeOption.getOptionKey() == null || quoteAttributeOption.getOptionKey() == "") {// 当不需要区分key的时候设置成same
            quoteAttributeOption.setOptionKey("same");
        }
        String printModeType = "single,back,roll";

        if (quoteAttributeOption.getReprints() != null) {
            if (quoteAttributeOption.getReprints()) {
                printModeType += ",reprints";
            }
            quoteAttributeOption.setPrintModeType(printModeType);
        }

        return this.insert(quoteAttributeOption);
    }

}
